package com.IClass.IActivity;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map.Entry;

import com.IClass.IClassApi;
import com.IClass.ImageUtil;
import com.IClass.NetUploadPictureListen;
import com.IClass.NetUtil;
import com.IClass.App.IClass;
import com.IClass.App.R;
import com.IClass.Server.AppJsonDefine.GenJavaCode.AppHttpHelp;
import com.IClass.image.AsynImageLoader;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.Toast;

public class ILookUploadpictureActivity extends Activity
{
	private static int GETUPLOADLOOKPICTUREOK = 1000;
	private static int SHOWIMAGE = 1001;
	private static int SAVEPICTURERESULT = 1002;
	private static int GETPICTUREFAIL = 1003;
	public WebView commentview;
	public AsynImageLoader asynimageloader = new AsynImageLoader();
	public Handler mhandle = new Handler();
	public String userid;
	public String strImgPath;
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.ilook_uploadpic_comment_layout);
		
		
		SharedPreferences  share = this.getSharedPreferences("IClass", MODE_PRIVATE);  
        userid = share.getString("userid", "0");
        
        if(userid == "0")
        {
			Intent mainIntent = new Intent(ILookUploadpictureActivity.this,IRegisterActivity.class); 
            startActivity(mainIntent);    //启动Main界面
            return;
        	
        }
        
		commentview = (WebView) findViewById(R.id.webView1);
		
		// 获取上次数据
		SharedPreferences  share2 = this.getSharedPreferences("IClass", MODE_PRIVATE);  
		
        String GetUploadlookpicturedata = share2.getString("GetUploadlookpicture", "");
        
        if(!GetUploadlookpicturedata.equals(""))
        	IClass.GetPictureResutlData(GetUploadlookpicturedata);
        
        HashMap<String, String> picturestoresult = IClass.getPictureresult();
        if(picturestoresult.size() == 0)
        {
        	Toast.makeText(ILookUploadpictureActivity.this, "加载数据中!", Toast.LENGTH_LONG).show();
        	// 加载数据线程
    		new Thread()
    		{
    			@Override
                public void run() {
    				
    		        	
    		        	final String result = IClass.classapi.GetUploadlookpicture(userid);
    		        	
    		        	if(result.equals(""))
    		        	{
    		        		mhandle.post(new Runnable() {
    							@Override
    							public void run() {
    								Toast.makeText(ILookUploadpictureActivity.this, "加载数据失败!", Toast.LENGTH_SHORT).show();
    							}

    						});
    						
    		        		return;
    		        	}
    		        	
    		        	
    		        	mhandle.post(new Runnable() {
    						@Override
    						public void run() {
    							Editor sharedata = getSharedPreferences("IClass", MODE_PRIVATE).edit();  
    							sharedata.putString("GetUploadlookpicture",result);
    							sharedata.commit();
    							
    						}

    					});
    		        	
    		        	HashMap<String, String> picturestoresult = IClass.getPictureresult();	
    					for(Entry<String, String> entry:picturestoresult.entrySet())
    					{
    						asynimageloader.loadimageurl(IClassApi.SERVER_HOST, entry.getKey());
    						
    					}
    					mhandle.post(new Runnable() {
    						@Override
    						public void run() {
    							OnLoadUpPictureOk();
    							
    						}

    					});
    					

    				
    			}
    			
    		}.start();
        }
        else
        {
        	OnLoadUpPictureOk();
        }
		
		
		
	}
	public void OnLoadUpPictureOk()
	{
		commentview.getSettings().setDefaultTextEncodingName("utf-8") ;
		commentview.getSettings().setJavaScriptEnabled(true);
		commentview.getSettings().setAllowFileAccess(true);
		
		commentview.addJavascriptInterface(new Object() 
		{  
            public void clickimage(String picnpath) 
            {  
            	
				strImgPath = Environment.getExternalStorageDirectory().toString();
		        strImgPath += "/ioclass/imagescach/" + picnpath;
		        mhandle.post(new Runnable() {
					@Override
					public void run() {
						final View vPopupWindow = getLayoutInflater().inflate(R.layout.iask_blogpictureview,null,false);                               // 加载popupWindow的布局文件     
						//vPopupWindow.setBackgroundColor(Color.GREEN);
						
						final PopupWindow pw = new PopupWindow(vPopupWindow, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, true);
						pw.setContentView(vPopupWindow);
						
						
						ImageView imageview = (ImageView) vPopupWindow.findViewById(R.id.imageView1);
						//String bigpictureurl = IClassApi.SERVER_HOST +  (String)blogdata.get("image_1");
						//(String) arg1.getTag();
						//http://ltfll.gicp.net:8085/data/attachment/album/201205/17/181253za22ntxox14tzntn.jpg
							//http://ltfll.gicp.net:8085/data/attachment/album/201205/17/181253za22ntxox14tzntn.jpg.thumb.jpg
						//bigpictureurl = bigpictureurl.substring(0, bigpictureurl.lastIndexOf(".thumb.jpg"));
						
						//imageview.setTag(bigpictureurl);
						//mImageAsynLoader.loadBitmap(imageview,  ImageUtil.getBitmapById(context, R.drawable.icon_3_n));
						imageview.setImageBitmap(ImageUtil.getBitmapByPath(strImgPath));
						vPopupWindow.setOnKeyListener(new OnKeyListener() 
						{
					            
					            @Override
					            public boolean onKey(View v, int keyCode, KeyEvent event) {
					                // TODO Auto-generated method stub
					                if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK)
					                	pw.dismiss();
					                return false;
					            }
					        });
						vPopupWindow.setOnClickListener(new OnClickListener()
						{

							@Override
							public void onClick(View v) {
								// TODO Auto-generated method stub
								pw.dismiss();
							}
							
						});
						pw.showAtLocation(commentview, Gravity.CENTER, 0, 0);// 以屏幕中心为参照，不偏移
						
					}

				});
				
            }  
        }, "demo");  
		
		HashMap<String, String> picturestoresult = IClass.getPictureresult();
		
		
		String data = "<html><body><table rules=\"rows\" cellpadding=\"5\" width=\"100&#37\">";
		
		SimpleDateFormat f1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		String strImgPath = Environment.getExternalStorageDirectory().toString();
        strImgPath += "/ioclass/imagescach/";
        
		for(Entry<String, String> entry:picturestoresult.entrySet())
		{
			//loadDataWithBaseURL  (String baseUrl, String data, String mimeType, String encoding, String historyUrl)
			//commentview.loadDataWithBaseURL("file://" + strImgPath,"<img src=\""  + "ffffff.jpg" + "\"></img>","jpg","utf-8","");
			//String filename = entry.getKey().substring(entry.getKey().lastIndexOf("/") + 1, entry.getKey().length());
			
			String text = asynimageloader.loadservertext(IClassApi.SERVER_HOST , entry.getValue() + ".txt");//IClass.classapi.GetServerText(IClassApi.SERVER_HOST + entry.getValue() + ".txt");
			
			
			data = data + "<tr>";
			data = data + "<td valign=\"top\" width=\"10&#37\">" + "<img src=\""  +  entry.getKey() + "\" height=\"60\" width=\"60\" onClick=\"window.demo.clickimage('" + entry.getKey() + "')\"></img>" + "</td>";
			data = data + "<td>" + text + "</font></td></hr>";
			data = data + "</tr>";
		}
		data = data + "</table></body></html>";	
		
		//commentview.loadDataWithBaseURL("file:///mnt/sdcard/ioclass/images/","<html><body><img src=\""  + "ffffff.jpg" + "\"></img></body></html>","text/html","utf-8",""); 
		//commentview.loadData(data, "text/html", "utf-8"); 
		commentview.loadDataWithBaseURL("file://" + strImgPath,data,"text/html","utf-8","");
		//commentview.loadDataWithBaseURL(IClassApi.SERVER_HOST,data,"text/html","utf-8","");
		//commentview.loadData(data, "text/html", "utf-8");
	}

}
